﻿using BLL;
using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HslCommunication;
using HslCommunication.Profinet.Siemens;
using HpSocketFunction;
using SerialPortFunction;

namespace MachineDemo
{
    public partial class ConnTestForm : Form
    {
        public ConnTestForm()
        {
            InitializeComponent();

            #region PLC信息
            DeviceConnMessage PLCConnMessage= LogicSingle.Instaince. _DeviceConnList.Where(x => x.DeviceName == "PLC").FirstOrDefault();
            txb_PLCIP.Text= PLCConnMessage.ServerAdress.ToString();
            #endregion

            #region CCD信息
            DeviceConnMessage CCDConnMessage = LogicSingle.Instaince._DeviceConnList.Where(x => x.DeviceName == "CCD客户端").FirstOrDefault();
            txb_CCDServerIP.Text = CCDConnMessage.ServerAdress.ToString();
            txb_CCDServerPort.Text = CCDConnMessage.ServerPort.ToString();
            txb_CCDBindIP.Text = CCDConnMessage.BindAdress.ToString();
            txb_CCDBindPort.Text = CCDConnMessage.BindPort.ToString();
            #endregion

            #region 服务器信息
            DeviceConnMessage UpComputerConnMessage = LogicSingle.Instaince._DeviceConnList.Where(x => x.DeviceName == "上位机客户端").FirstOrDefault();
            txb_UperServerIP.Text= UpComputerConnMessage.ServerAdress.ToString();
            txb_UperServerPort.Text = UpComputerConnMessage.ServerPort.ToString();
            txb_UperBindIP.Text = UpComputerConnMessage.BindAdress.ToString();
            txb_UperBindPort.Text = UpComputerConnMessage.BindPort.ToString();
            #endregion

            #region 机器人信息
            DeviceConnMessage RobotConnMessage = LogicSingle.Instaince._DeviceConnList.Where(x => x.DeviceName == "机器人客户端").FirstOrDefault();
            txb_RobotServerIP.Text = RobotConnMessage.ServerAdress.ToString();
            txb_RobotServerPort.Text = RobotConnMessage.ServerPort.ToString();
            txb_RobotBindIP.Text= RobotConnMessage.BindAdress.ToString();
            txb_RobotBindPort.Text = RobotConnMessage.BindPort.ToString();
            #endregion

            #region 扫码抢信息
            DeviceConnMessage ScanConnMessage = LogicSingle.Instaince._DeviceConnList.Where(x => x.DeviceName == "扫码抢客户端").FirstOrDefault();
            txb_ScanServerIP.Text = ScanConnMessage.ServerAdress.ToString();
            txb_ScanServerPort.Text = ScanConnMessage.ServerPort.ToString();
            txb_ScanBindIP.Text = ScanConnMessage.BindAdress.ToString();
            txb_ScanBindPort.Text = ScanConnMessage.BindPort.ToString();
            #endregion

            #region 测高信息
            DeviceConnMessage HeightConnMessage = LogicSingle.Instaince._DeviceConnList.Where(x => x.DeviceName == "测高串口").FirstOrDefault();
            txb_HeightPortName.Text = HeightConnMessage.PortName.ToString();
            txb_HeightParity.Text = HeightConnMessage.Parity.ToString();
            txb_HeightDataBits.Text = HeightConnMessage.DataBits.ToString();
            txb_HeightStopBits.Text = HeightConnMessage.StopBits.ToString();
            txb_HeightBaudRate.Text = HeightConnMessage.BaudRate.ToString();
            txb_HeightMessageMode.Text = HeightConnMessage.ComMessageMode.ToString();
            #endregion

            #region 测重信息
            DeviceConnMessage WeightConnMessage = LogicSingle.Instaince._DeviceConnList.Where(x => x.DeviceName == "测重串口").FirstOrDefault();
            txb_WeightPortName.Text = WeightConnMessage.PortName.ToString();
            txb_WeightParity.Text = WeightConnMessage.Parity.ToString();
            txb_WeightDataBits.Text = WeightConnMessage.DataBits.ToString();
            txb_WeightStopBits.Text = WeightConnMessage.StopBits.ToString();
            txb_WeightBaudRate.Text = WeightConnMessage.BaudRate.ToString();
            txb_WeightMessageMode.Text = WeightConnMessage.ComMessageMode.ToString();
            #endregion
        }
        #region 读取PLC地址
        /// <summary>
        /// 读取PLC地址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_ReadPlc_Click(object sender, EventArgs e)
        {
            if (LogicSingle.Instaince._PlcConnect.IsSuccess)
            {
                try
                {

                    foreach (Control item in this.tanP_PLCType.Controls)
                    {
                        if (item is RadioButton)
                        {
                            if (((RadioButton)item).Checked)
                            {
                                switch (item.Tag.ToString())
                                {
                                    case "PLCBool":
                                        txb_IOValue.Text = LogicSingle.Instaince.ReadMessageToPlc(txb_IOAdress.Text, PLCDataType.PLCBool).ToString();
                                        break;
                                    case "PLCByte":
                                        txb_IOValue.Text = LogicSingle.Instaince.ReadMessageToPlc(txb_IOAdress.Text, PLCDataType.PLCByte).ToString();
                                        break;
                                    case "PLCInt16":
                                        txb_IOValue.Text = LogicSingle.Instaince.ReadMessageToPlc(txb_IOAdress.Text, PLCDataType.PLCInt16).ToString();
                                        break;
                                    case "PLCUInt16":
                                        txb_IOValue.Text = LogicSingle.Instaince.ReadMessageToPlc(txb_IOAdress.Text, PLCDataType.PLCUInt16).ToString();
                                        break;
                                    case "PLCInt32":
                                        txb_IOValue.Text = LogicSingle.Instaince.ReadMessageToPlc(txb_IOAdress.Text, PLCDataType.PLCInt32).ToString();
                                        break;
                                    case "PLCUInt32":
                                        txb_IOValue.Text = LogicSingle.Instaince.ReadMessageToPlc(txb_IOAdress.Text, PLCDataType.PLCUInt32).ToString();
                                        break;
                                    case "PLCFloat":
                                        txb_IOValue.Text = LogicSingle.Instaince.ReadMessageToPlc(txb_IOAdress.Text, PLCDataType.PLCFloat).ToString();
                                        break;
                                    case "PLCInt64":
                                        txb_IOValue.Text = LogicSingle.Instaince.ReadMessageToPlc(txb_IOAdress.Text, PLCDataType.PLCInt64).ToString();
                                        break;
                                    case "PLCUInt64":
                                        txb_IOValue.Text = LogicSingle.Instaince.ReadMessageToPlc(txb_IOAdress.Text, PLCDataType.PLCUInt64).ToString();
                                        break;
                                    case "PLCDouble":
                                        txb_IOValue.Text = LogicSingle.Instaince.ReadMessageToPlc(txb_IOAdress.Text, PLCDataType.PLCDouble).ToString();
                                        break;
                                    case "PLCString":
                                        txb_IOValue.Text = LogicSingle.Instaince.ReadMessageToPlc(txb_IOAdress.Text, PLCDataType.PLCString).ToString();
                                        break;
                                    default:
                                        break;     
                                }
                                AddLog(listV_PLCTestLog, "读取" + txb_IOAdress.Text + "为" + txb_IOValue.Text);
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    AddLog(listV_PLCTestLog, "读取失败");
                    throw;
                }
            }
            else
            {
                AddLog(listV_PLCTestLog, "未连接plc,请检查网络");
            }
        }
        #endregion

        #region 写入PLC地址

        /// <summary>
        /// 写入PLC地址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_WritePlc_Click(object sender, EventArgs e)
        {
            if (txb_PLCIP.Text == null || txb_IOAdress.Text == "" || txb_IOValue.Text == "")
            {
                MessageBox.Show("不能为空");
                return;
            }
            if (!LogicSingle.Instaince._PlcConnect.IsSuccess)
            {
                AddLog(listV_PLCTestLog, "未连接plc,请检查网络");
                return;
            }
            try
            {
                foreach (Control item in this.tanP_PLCType.Controls)
                {

                    if (item is RadioButton)
                    {
                        if (((RadioButton)item).Checked)
                        {
                           
                            switch (item.Tag.ToString())
                            {
                                case "PLCBool":
                                    LogicSingle.Instaince.SendMessageToPlc(txb_IOAdress.Text, PLCDataType.PLCBool, Convert.ToBoolean(txb_IOValue.Text));
                                    break;
                                case "PLCByte":
                                    LogicSingle.Instaince.SendMessageToPlc(txb_IOAdress.Text, PLCDataType.PLCByte, Convert.ToByte(txb_IOValue.Text));
                                    break;
                                case "PLCInt16":
                                    LogicSingle.Instaince.SendMessageToPlc(txb_IOAdress.Text, PLCDataType.PLCInt16, Convert.ToInt16(txb_IOValue.Text));
                                    break;
                                case "PLCUInt16":
                                    LogicSingle.Instaince.SendMessageToPlc(txb_IOAdress.Text, PLCDataType.PLCUInt16, Convert.ToUInt16(txb_IOValue.Text));
                                    break;
                                case "PLCInt32":
                                    LogicSingle.Instaince.SendMessageToPlc(txb_IOAdress.Text, PLCDataType.PLCInt32, Convert.ToInt32(txb_IOValue.Text));
                                    break;
                                case "PLCUInt32":
                                    LogicSingle.Instaince.SendMessageToPlc(txb_IOAdress.Text, PLCDataType.PLCUInt32, Convert.ToUInt32(txb_IOValue.Text));
                                    break;
                                case "PLCFloat":
                                    LogicSingle.Instaince.SendMessageToPlc(txb_IOAdress.Text, PLCDataType.PLCFloat, Convert.ToDouble(txb_IOValue.Text));
                                    break;
                                case "PLCInt64":
                                    LogicSingle.Instaince.SendMessageToPlc(txb_IOAdress.Text, PLCDataType.PLCInt64, Convert.ToInt64(txb_IOValue.Text));
                                    break;
                                case "PLCUInt64":
                                    LogicSingle.Instaince.SendMessageToPlc(txb_IOAdress.Text, PLCDataType.PLCUInt64, Convert.ToUInt64(txb_IOValue.Text));
                                    break;
                                case "PLCDouble":
                                    LogicSingle.Instaince.SendMessageToPlc(txb_IOAdress.Text, PLCDataType.PLCDouble, Convert.ToDouble(txb_IOValue.Text));
                                    break;
                                case "PLCString":
                                    LogicSingle.Instaince.SendMessageToPlc(txb_IOAdress.Text, PLCDataType.PLCString, txb_IOValue.Text);
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                //AddLog(listV_PLCTestLog, "写入失败");
                return;
                throw;
            }
            AddLog(listV_PLCTestLog, "写入成功");
        }

        #endregion

        #region 发送CCD
        private void btn_SendCCD_Click(object sender, EventArgs e)
        {
            TcpSocketClientSend(LogicSingle.Instaince._CCDClientent, txb_SendCCDMessage, listV_CCDTestLog);
        }
        #endregion

        #region 发送服务器
        private void btn_SendServer_Click(object sender, EventArgs e)
        {
            TcpSocketClientSend(LogicSingle.Instaince._UperComputerClientent, txb_SendServerMessage, listV_UperTestLog);
        }
        #endregion

        #region 发送机器人
        private void btn_SendRobot_Click(object sender, EventArgs e)
        {
            TcpSocketClientSend(LogicSingle.Instaince._RobotClient, txb_SendRobotMessage, listV_RobotTestLog);
        }
        #endregion

        #region 发送扫码抢
        private void btn_SendScan_Click(object sender, EventArgs e)
        {
            TcpSocketClientSend(LogicSingle.Instaince._ScanClient, txb_SendScanMessage, listV_ScanTestLog);
        }
        #endregion

        #region 发送测高
        private void btn_SendHeight_Click(object sender, EventArgs e)
        {
            ComSend(LogicSingle.Instaince._HeightDevice, txb_SendHeightMessage, listV_HeightTestLog);
        }
        #endregion

        #region 发送测重
        private void btn_SendWeight_Click(object sender, EventArgs e)
        {
            ComSend(LogicSingle.Instaince._WeightDevice, txb_SendWeightMessage, listV_WeightTestLog);
        }
        #endregion

        #region 串口发送处理
        public void ComSend(SerialHelper DeciceCom, TextBox DataBox, ListView ListViewLog)
        {
            if (!DeciceCom.IsOpen)
            {
                AddLog(ListViewLog, "连接失败，请检查串口连接");
                return;
            }
            try
            {
                bool res = DeciceCom.SendData(DataBox.Text);
                if (res)
                {
                    AddLog(ListViewLog, "发送成功");
                }
                else
                {
                    AddLog(ListViewLog, "发送失败");
                }
            }
            catch (Exception ex)
            {
                AddLog(ListViewLog, "发送失败" + ex.Message);
            }
        }
        #endregion

        #region 客户端发送处理
        public void TcpSocketClientSend(HpSocketClient DeviceClient,TextBox DataBox,ListView ListViewLog)
        {
            if (!DeviceClient.IsConnection)
            {
                AddLog(ListViewLog, "连接失败，请检查网络连接");
                return;
            }
            try
            {
                bool res = DeviceClient.SendData(DataBox.Text);
                if (res)
                {
                    AddLog(ListViewLog, "发送成功");
                }
                else
                {
                    AddLog(ListViewLog, "发送失败");
                }
            }
            catch (Exception ex)
            {
                AddLog(ListViewLog, "发送失败" + ex.Message);
            }
        }
        #endregion

        #region 日志追加
        public void AddLog(ListView listview, string log)
        {
            if (listview.InvokeRequired)
            {
                Invoke(new Action(() =>
                {
                    ListViewItem listviewitem = new ListViewItem(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    listviewitem.SubItems.Add(log);
                    listview.Items.Insert(0, listviewitem);
                }));
            }
            else
            {
                ListViewItem listviewitem = new ListViewItem(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                listviewitem.SubItems.Add(log);
                listview.Items.Insert(0, listviewitem);
            }
        }
        #endregion
    }
}
